package com.example.demo.Utils;

import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * Created by hasee
 * on 2019/11/19 18:52
 */
public class PasswordEncrypt {

    //对用户的密码加密
    public String encrypt(String password){
        BCryptPasswordEncoder encoder =new BCryptPasswordEncoder();
//        System.out.println(encoder.encode(password));
        String hashpw = BCrypt.hashpw(password,BCrypt.gensalt());
        System.out.println(hashpw);
        return hashpw;
    }

    //验证token中的密码信息和数据库中已加密的密码
    public boolean match(String password,String rawPassword){
        boolean matchResult = false;
        BCryptPasswordEncoder encoder =new BCryptPasswordEncoder();
//        matchResult = encoder.matches(rawPassword,password);
        matchResult = BCrypt.checkpw(password,rawPassword);
        System.out.println("密码验证结果:"+matchResult);
        return matchResult;
    }
}
